package com.ruoyi.web.controller.train;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GfSeat;
import com.ruoyi.system.domain.GfTrain;
import com.ruoyi.system.service.IGfSeatService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.IGfTrainService;

/**
 * 座位信息Controller
 * 
 * @author ruoyi
 * @date 2022-06-16
 */
@RestController
@RequestMapping("/system/seat")
public class GfSeatController extends BaseController
{
    @Autowired
    private IGfSeatService gfSeatService;

    /**
     * 查询座位信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:seat:list')")
    @GetMapping("/list")
    public TableDataInfo list(GfSeat gfSeat)
    {
        startPage();
        List<GfSeat> list = gfSeatService.selectGfSeatList(gfSeat);
        return getDataTable(list);
    }

    /**
     * 导出座位信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:seat:export')")
    @Log(title = "座位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GfSeat gfSeat)
    {
        List<GfSeat> list = gfSeatService.selectGfSeatList(gfSeat);
        ExcelUtil<GfSeat> util = new ExcelUtil<GfSeat>(GfSeat.class);
        util.exportExcel(response, list, "座位信息数据");
    }


    @Autowired
    IGfTrainService trainService;
    /**
     * 获取座位信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:seat:query')")
    @GetMapping(value = "/{seatId}")
    public AjaxResult getInfo(@PathVariable("seatId") Long seatId)
    {
        GfSeat data = gfSeatService.selectGfSeatBySeatId(seatId);
        GfTrain gfTrain = trainService.selectByTrainNumber(data.getTrainId());
        if (gfTrain == null) {
            return error("此座位对应的动车信息不存在,您可在[动车信息]菜单中,查看是否编号为" + data.getTrainId() + "的动车.");
        }
        return AjaxResult.success(data);
    }

    /**
     * 新增座位信息
     */
    @PreAuthorize("@ss.hasPermi('system:seat:add')")
    @Log(title = "座位信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GfSeat gfSeat)
    {
        return toAjax(gfSeatService.insertGfSeat(gfSeat));
    }

    /**
     * 修改座位信息
     */
    @PreAuthorize("@ss.hasPermi('system:seat:edit')")
    @Log(title = "座位信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GfSeat gfSeat)
    {
        return toAjax(gfSeatService.updateGfSeat(gfSeat));
    }

    /**
     * 删除座位信息
     */
    @PreAuthorize("@ss.hasPermi('system:seat:remove')")
    @Log(title = "座位信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{seatIds}")
    public AjaxResult remove(@PathVariable Long[] seatIds)
    {
        return toAjax(gfSeatService.deleteGfSeatBySeatIds(seatIds));
    }
}
